

var dataList;
var MAX_NEWS_INLINE = 3;
var viewModel = new function () {
    
    
    
    /////////////////////// Get some list product ////////////////////////
    //////////////////////////////////////////////////////////////////////
    var dataPost = {};
    this.listInfo = ko.observableArray();
    this.listInfo1 = ko.observableArray();
    this.listInfo2 = ko.observableArray();
    this.listDiscount = ko.observableArray();
    this.listDiscount1 = ko.observableArray();
    this.listDiscount2 = ko.observableArray();
    this.listOther = ko.observableArray();
    this.listOther1 = ko.observableArray();
    this.listOther2 = ko.observableArray();
    
    function NewItem(_Id, _CategoryId, _ImagePath, _CreateDate, _Title, _Summary) {
        this.Id = _Id;
        this.CategoryId = _CategoryId
        this.ImagePath = _ImagePath;
        this.Link = "NewsDetail.aspx?newsid=" + _Id + "&cateid=" + _CategoryId;
        this.CreateDate = _CreateDate;
        this.Title = _Title;
        this.Summary = _Summary;
    }
    // Get list tin tuc chung
    var urlPost = "but/ajax/TinTuc.aspx?type=topnews&num=12&cateid=2";
    $.ajax({ type: "POST", url: urlPost, returnData: dataPost, dataType: "json",
        success: function (returnData, textStatus, jqXHR) {
            writeLog(returnData);
            if(returnData.length > 0) {
                var newsMax = returnData.length;
                
                for(var i=0; i<returnData.length; i++) {
                    viewModel.listInfo.push(new NewItem(    returnData[i].NewsId,
                                                            returnData[i].CategoryId,
                                                            returnData[i].ImagePath.replace("_s.",".").replace("../",""), 
                                                            returnData[i].Createdate,
                                                            returnData[i].Title,
                                                            returnData[i].Summary));
                }
                
                if(newsMax >= MAX_NEWS_INLINE) newsMax = MAX_NEWS_INLINE;
                
                if(newsMax > 0) {
                    for(var i=0; i<newsMax; i++) {
                        viewModel.listInfo1.push(viewModel.listInfo()[i]);
                    }
                }
                
                if(newsMax >= MAX_NEWS_INLINE) {
                    for(var i=newsMax; i<returnData.length; i++) {
                        viewModel.listInfo2.push(viewModel.listInfo()[i]);
                    }
                }
            }
            else writeLog("news-cateid=2 is null");
        },
        error: function (xhr, status, error) {

        }
    });  // End ajax post*/
    
    
    // Get list tin khuyen mai
    var urlPost = "but/ajax/TinTuc.aspx?type=topnews&num=12&cateid=3";
    $.ajax({ type: "POST", url: urlPost, returnData: dataPost, dataType: "json",
        success: function (returnData, textStatus, jqXHR) {
            writeLog(returnData);
            if(returnData.length > 0) {
                var newsMax = returnData.length;
                
                for(var i=0; i<returnData.length; i++) {
                    viewModel.listDiscount.push(new NewItem(    returnData[i].NewsId,
                                                            returnData[i].CategoryId,
                                                            returnData[i].ImagePath.replace("_s.",".").replace("../",""), 
                                                            returnData[i].Createdate,
                                                            returnData[i].Title,
                                                            returnData[i].Summary));
                }
                
                if(newsMax >= MAX_NEWS_INLINE) newsMax = MAX_NEWS_INLINE;
                
                if(newsMax > 0) {
                    for(var i=0; i<newsMax; i++) {
                        viewModel.listDiscount1.push(viewModel.listDiscount()[i]);
                    }
                }
                
                if(newsMax >= MAX_NEWS_INLINE) {
                    for(var i=newsMax; i<returnData.length; i++) {
                        viewModel.listDiscount2.push(viewModel.listDiscount()[i]);
                    }
                }
            }
            else writeLog("news-cateid=3 is null");
        },
        error: function (xhr, status, error) {

        }
    });  // End ajax post*/
    
    
    // Get list tin khuyen mai
    var urlPost = "but/ajax/TinTuc.aspx?type=topnews&num=12&cateid=3";
    $.ajax({ type: "POST", url: urlPost, returnData: dataPost, dataType: "json",
        success: function (returnData, textStatus, jqXHR) {
            writeLog(returnData);
            if(returnData.length > 0) {
                var newsMax = returnData.length;
                
                for(var i=0; i<returnData.length; i++) {
                    viewModel.listOther.push(new NewItem(    returnData[i].NewsId,
                                                            returnData[i].CategoryId,
                                                            returnData[i].ImagePath.replace("_s.",".").replace("../",""), 
                                                            returnData[i].Createdate,
                                                            returnData[i].Title,
                                                            returnData[i].Summary));
                }
                
                if(newsMax >= MAX_NEWS_INLINE) newsMax = MAX_NEWS_INLINE;
                
                if(newsMax > 0) {
                    for(var i=0; i<newsMax; i++) {
                        viewModel.listOther1.push(viewModel.listOther()[i]);
                    }
                }
                
                if(newsMax >= MAX_NEWS_INLINE) {
                    for(var i=newsMax; i<returnData.length; i++) {
                        viewModel.listOther2.push(viewModel.listOther()[i]);
                    }
                }
            }
            else writeLog("news-cateid=3 is null");
        },
        error: function (xhr, status, error) {

        }
    });  // End ajax post*/
    
};

ko.applyBindings(viewModel, document.getElementById('viewNews'));

